﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using ManagerDisplay.PetroFexManagerServiceReference;
using ManagerDisplay.PetroFexFuelSupplyServiceReference;
using System.Globalization;

namespace ManagerDisplay
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        System.Timers.Timer updateTimer = new System.Timers.Timer(5000); //much slower than POS one as it's not necessary to check that often for this purpose
        PetroFexServiceClient stationServerClient = new PetroFexServiceClient();
        FuelSupplyServiceClient fuelSupplyClient = new FuelSupplyServiceClient();
        public MainWindow()
        {
            InitializeComponent();
            stationServerClient.ConnectManagerSystem();
            var updaterThread = new ThreadStart(updateTankLevels);
            updaterThread.BeginInvoke(null, null);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            stationServerClient.DisconnectManagerSystem();
            stationServerClient.Close();
        }

        private void updateTankLevels()
        {
            updateTimer.Elapsed += (sender, args) =>
            {
                List<float> levels = stationServerClient.GetPetrolTankLevels();
                Application.Current.Dispatcher.BeginInvoke(new Action(() => unleadedLevelBar.Value = (int)levels[0]));
                Application.Current.Dispatcher.BeginInvoke(new Action(() => dieselLevelBar.Value = (int)levels[1]));
                Application.Current.Dispatcher.BeginInvoke(new Action(() => unleadedTankLevelLbl.Content = levels[0].ToString("N", new CultureInfo("en-GB")) + " Litres"));
                Application.Current.Dispatcher.BeginInvoke(new Action(() => dieselTankLevelLbl.Content = levels[1].ToString("N", new CultureInfo("en-GB")) + " Litres"));
            };
            updateTimer.Start();
        }

        private void getPriceQuoteBtn_Click(object sender, RoutedEventArgs e)
        {
            FuelPriceQuote quote = fuelSupplyClient.GetFuelPrices(201009271);
            List<FuelPrice> prices = quote.QuotePrices;
            Application.Current.Dispatcher.BeginInvoke(new Action(() => dieselPriceLbl.Content = prices[0].Price.ToString("C", new CultureInfo("en-GB"))));
            Application.Current.Dispatcher.BeginInvoke(new Action(() => unleadedPriceLbl.Content = prices[1].Price.ToString("C", new CultureInfo("en-GB"))));
            DateTime quoteTime = quote.QuoteDate;
            Application.Current.Dispatcher.BeginInvoke(new Action(() => quoteDateTimeLbl.Content = quoteTime.ToShortDateString() + " - " + quoteTime.ToShortTimeString())); 
           
            Guid quoteRef = quote.QuoteReference;
            Application.Current.Dispatcher.BeginInvoke(new Action(() => quoteRefGuidLbl.Content = quoteRef.ToString()));
        }
        private void setPricesBtn_Click(object sender, RoutedEventArgs e)
        {
            float unleadedResult = 0.0f;
            float dieselResult = 0.0f;
            bool tryParse = float.TryParse(setUnleadedPriceBox.Text, out unleadedResult);
            if (!tryParse)
            {
                MessageBox.Show("Incorrect Unleaded Price Entered!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            tryParse = float.TryParse(setDieselPriceBox.Text, out dieselResult);
            if (!tryParse)
            {
                MessageBox.Show("Incorrect Diesel Price Entered!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            stationServerClient.SetFuelPrices(unleadedResult, dieselResult);
        }
        private void getSalesBtn_Click(object sender, RoutedEventArgs e)
        {
            DateTime? nullableDate = getFuelSaleDatePicker.SelectedDate;
            if (nullableDate == null)
            {
                MessageBox.Show("Invalid Date Entered", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            DateTime date = nullableDate.Value;
            string dateStr = date.ToShortDateString();

            List<float> sales = stationServerClient.GetFuelSales(dateStr, getSalesFuelBox.Text);
            float totalLitres = 0.0f;
            float totalPaid = 0.0f;
            for (int i = 0; i < sales.Count; i += 2)
            {
                totalLitres += sales[i];
                totalPaid += sales[i + 1];
            }
            litresSoldValueLbl.Content = totalLitres.ToString("N", new CultureInfo("en-GB"));
            totalPaidValueLbl.Content = "£" + totalPaid.ToString("N", new CultureInfo("en-GB"));
        }
    }
}
